import _ from 'lodash'
import {article, section} from './lablog.js'
import fs from 'fs'
import folktale from 'folktale'
const {task} = folktale.concurrency.task

// console.log(task)
article("与 IO 类似，Task 也是将对未来的操作的指示放到了一个时间胶囊里，就像家务列表那样。")


const split = _.curryRight((str, delimiter) => _.split(str, delimiter))
const slice = _.curry((start, end, array) => _.slice(array, start, end))
const sliceTwo = slice(0,2)

const readFile = function(filename) {
    return task(function(resolver) {
        fs.readFile(filename, 'utf-8', function(err, data) {
            err ? resolver.reject(err) : resolver.resolve(data)
        })
    })
}

const headData = readFile("package2.json").map(split("\n")).map(sliceTwo)
headData.run().listen({
    onResolved: (data) => { console.log("data:", data) },
    onRejected: (err) => { console.error("exception:", err) }
})
